<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Python 并发编程 | KnightSama‘s Blog</title>
    <meta name="description" content="Hello, my friend!">
    <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
  <script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@babel/standalone/babel.min.js"></script>
    
    <link rel="preload" href="/assets/css/0.styles.c4ea52a5.css" as="style"><link rel="preload" href="/assets/js/app.a2483de7.js" as="script"><link rel="preload" href="/assets/js/4.0078dadf.js" as="script"><link rel="preload" href="/assets/js/1.c3e7d2b0.js" as="script"><link rel="preload" href="/assets/js/21.1fe46223.js" as="script"><link rel="prefetch" href="/assets/js/10.265e4f40.js"><link rel="prefetch" href="/assets/js/11.a3a2f8c1.js"><link rel="prefetch" href="/assets/js/12.1910f8cc.js"><link rel="prefetch" href="/assets/js/13.229fa478.js"><link rel="prefetch" href="/assets/js/14.c27d4a17.js"><link rel="prefetch" href="/assets/js/15.92adafd4.js"><link rel="prefetch" href="/assets/js/16.bcee7c14.js"><link rel="prefetch" href="/assets/js/17.ee1bcf41.js"><link rel="prefetch" href="/assets/js/18.fa3c3a4e.js"><link rel="prefetch" href="/assets/js/19.1a943c57.js"><link rel="prefetch" href="/assets/js/20.44781dae.js"><link rel="prefetch" href="/assets/js/22.6f7d46c2.js"><link rel="prefetch" href="/assets/js/23.56223d02.js"><link rel="prefetch" href="/assets/js/24.e7cad43b.js"><link rel="prefetch" href="/assets/js/25.b2902552.js"><link rel="prefetch" href="/assets/js/26.70698c07.js"><link rel="prefetch" href="/assets/js/27.6ee3d263.js"><link rel="prefetch" href="/assets/js/28.ce131de6.js"><link rel="prefetch" href="/assets/js/29.90552487.js"><link rel="prefetch" href="/assets/js/30.b3291ccc.js"><link rel="prefetch" href="/assets/js/31.ea8c441e.js"><link rel="prefetch" href="/assets/js/32.3e29bbeb.js"><link rel="prefetch" href="/assets/js/33.65e78c5e.js"><link rel="prefetch" href="/assets/js/34.3ba19d7c.js"><link rel="prefetch" href="/assets/js/35.66d3fc4a.js"><link rel="prefetch" href="/assets/js/36.f5725281.js"><link rel="prefetch" href="/assets/js/37.f5b0b5ff.js"><link rel="prefetch" href="/assets/js/38.3cae3054.js"><link rel="prefetch" href="/assets/js/39.47dc1ffe.js"><link rel="prefetch" href="/assets/js/5.f0714e2e.js"><link rel="prefetch" href="/assets/js/6.53240a5f.js"><link rel="prefetch" href="/assets/js/7.dc126a6b.js"><link rel="prefetch" href="/assets/js/8.86452400.js"><link rel="prefetch" href="/assets/js/9.16ef06de.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.734d5d92.js">
    <link rel="stylesheet" href="/assets/css/0.styles.c4ea52a5.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-0cb1c278><div data-v-0cb1c278><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-0cb1c278 data-v-0cb1c278><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-da0660ba data-v-0cb1c278 data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>KnightSama‘s Blog</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div class="hide" data-v-0cb1c278><header class="navbar" data-v-0cb1c278><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/Misaka.jpg" alt="KnightSama‘s Blog" class="logo"> <span class="site-name">KnightSama‘s Blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-0cb1c278></div> <aside class="sidebar" data-v-0cb1c278><div class="personal-info-wrapper" data-v-4a3def3c><img src="/Misaka.jpg" alt="author-avatar" class="personal-img" data-v-4a3def3c> <h3 class="name" data-v-4a3def3c>
    KnightSama
  </h3> <div class="num" data-v-4a3def3c><div data-v-4a3def3c><h3 data-v-4a3def3c>27</h3> <h6 data-v-4a3def3c>文章</h6></div> <div data-v-4a3def3c><h3 data-v-4a3def3c>14</h3> <h6 data-v-4a3def3c>标签</h6></div></div> <hr data-v-4a3def3c></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Python 并发编程</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Views/20190509.html#多进程" class="sidebar-link">多进程</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20190509.html#fork" class="sidebar-link">fork()</a></li><li class="sidebar-sub-header"><a href="/Views/20190509.html#multiprocessing" class="sidebar-link">multiprocessing</a></li><li class="sidebar-sub-header"><a href="/Views/20190509.html#进程池" class="sidebar-link">进程池</a></li><li class="sidebar-sub-header"><a href="/Views/20190509.html#自定义进程类" class="sidebar-link">自定义进程类</a></li><li class="sidebar-sub-header"><a href="/Views/20190509.html#进程间通讯" class="sidebar-link">进程间通讯</a></li></ul></li><li><a href="/Views/20190509.html#多线程" class="sidebar-link">多线程</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20190509.html#thread" class="sidebar-link">Thread</a></li><li class="sidebar-sub-header"><a href="/Views/20190509.html#通过继承-thread-的方式创建线程" class="sidebar-link">通过继承 Thread 的方式创建线程</a></li><li class="sidebar-sub-header"><a href="/Views/20190509.html#线程池" class="sidebar-link">线程池</a></li></ul></li><li><a href="/Views/20190509.html#协程" class="sidebar-link">协程</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20190509.html#通过生成器-generator-实现" class="sidebar-link">通过生成器(Generator)实现</a></li><li class="sidebar-sub-header"><a href="/Views/20190509.html#通过-greenlet-实现" class="sidebar-link">通过 greenlet 实现</a></li><li class="sidebar-sub-header"><a href="/Views/20190509.html#通过-gevent-实现" class="sidebar-link">通过 gevent 实现</a></li></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-da0660ba data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>Python 并发编程</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div data-v-0cb1c278><main class="page"><div class="sidebar-open-close"><svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <!----> <div class="page-title" style="display:none;"><h1>Python 并发编程</h1> <hr> <div data-v-73367bb4><i class="iconfont reco-account" data-v-73367bb4><span data-v-73367bb4>KnightSama</span></i> <i class="iconfont reco-date" data-v-73367bb4><span data-v-73367bb4>2019-05-09</span></i> <i class="iconfont reco-eye" data-v-73367bb4><span id="/Views/20190509.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-73367bb4><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-73367bb4><span class="tag-item" data-v-73367bb4>
      Python
    </span><span class="tag-item" data-v-73367bb4>
      多线程
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="多进程"><a href="#多进程" class="header-anchor">#</a> 多进程</h2> <h3 id="fork"><a href="#fork" class="header-anchor">#</a> fork()</h3> <p>Unix/Linux 操作系统提供了一个 <code>fork()</code>  系统调用，它非常特殊。普通的函数调用，调用一次，返回一次，但是 <code>fork()</code>  调用一次，返回两次，因为操作系统自动把当前父进程复制了一份子进程，然后，分别在父进程和子进程内返回。
Python 的 os 模块封装了常见的系统调用，其中就包括 <code>fork</code> ，可以在 Python 程序中使用 <code>os.fork()</code>  轻松创建子进程，创建进程失败返回一个负数，子进程返回 0，而父进程返回子进程的 ID，调用 <code>fork()</code>  方法后，子进程被创建并会复制父进程的所有内存空间，并从下一句开始父子进程各自独立运行，由于父子进程 <code>fork()</code>  返回值有区别，配合 <code>if</code>  结构可以让父子进程执行不同的内容。父进程 <code>fork()</code>  之前开辟的空间子进程同样拥有，父子进程对各自空间的操作不会相互影响。</p> <table><thead><tr><th style="text-align:center;">相关方法</th> <th style="text-align:center;">简介</th></tr></thead> <tbody><tr><td style="text-align:center;"><code>os.fork()</code></td> <td style="text-align:center;">创建进程 <br> 失败返回负数，子进程返回 0，父进程返回子进程的 ID</td></tr> <tr><td style="text-align:center;"><code>os._exit(status)</code></td> <td style="text-align:center;">结束一个进程 <br> status 是进程的终止状态 <br> 如  <code>os._exit(0)</code></td></tr> <tr><td style="text-align:center;"><code>sys.exit(status)</code></td> <td style="text-align:center;">退出进程 <br>status 如果是整数则表示退出状态 <br>status 是字符串则表示退出时打印内容 <br> 如  <code>sys.exit(&quot;退出进程&quot;)</code></td></tr> <tr><td style="text-align:center;"><code>pid,status = os.wait()</code></td> <td style="text-align:center;">在父进程中阻塞等待处理子进程退出 <br> pid 退出的子进程的 PID <br> status 子进程退出状态</td></tr> <tr><td style="text-align:center;"><code>os.getpid()</code></td> <td style="text-align:center;">获得当前进程的进程 ID</td></tr> <tr><td style="text-align:center;"><code>os.getppid()</code></td> <td style="text-align:center;">获得父进程的进程 ID</td></tr></tbody></table> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> os
<span class="token comment"># pid是返回值，如果创建进程失败返回一个负数，子进程返回0，而父进程返回子进程的ID。</span>
pid <span class="token operator">=</span> os<span class="token punctuation">.</span>fork<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 后面的内容将在父进程与子进程中独立执行</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;父子进程都要执行&quot;</span><span class="token punctuation">)</span>
<span class="token comment"># 通过返回值来让父子进程执行不同的功能</span>
<span class="token keyword">if</span> pid <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;这里只有子进程会执行&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;这里只有父进程在执行&quot;</span><span class="token punctuation">)</span>
<span class="token comment"># 这里以后父子进程都会独立执行    </span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;父子进程都要执行&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><div class="custom-block tip"><p><strong>孤儿进程</strong></p> <p>父进程先于子进程退出，此时子进程成为孤儿进程。父进程退出之后，子进程会被 PID 为 1 的 init 进程接管，这样子进程就不会受终端退出影响了，使用这个特性就可以创建在后台执行的程序，俗称守护进程（daemon）。</p> <p><strong>僵尸进程</strong></p> <p>子进程先于父进程退出，父进程又没有处理子进程的退出状态，此时子进程就会称为僵尸进程。僵尸进程虽然结束，但是会存留部分 PCB 在内存中，大量的僵尸进程会浪费系统的内存资源。</p></div> <p><strong>如何避免僵尸进程</strong></p> <p>1、使用 <code>wait</code>  函数处理子进程退出</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> os
pid <span class="token operator">=</span> os<span class="token punctuation">.</span>fork<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> pid <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
    os<span class="token punctuation">.</span>_exit<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">#子进程退出</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    p<span class="token punctuation">,</span> status <span class="token operator">=</span> os<span class="token punctuation">.</span>wait<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 阻塞等待子进程退出</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>2、创建二级子进程处理僵尸
父进程创建子进程，等待回收子进程，子进程创建二级子进程然后退出，二级子进程变为孤儿进程，和原来父进程一同执行事件</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> os
pid <span class="token operator">=</span> os<span class="token punctuation">.</span>fork<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> pid <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
    p <span class="token operator">=</span> os<span class="token punctuation">.</span>fork<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 二级子进程</span>
    <span class="token keyword">if</span> p <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
      <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;这里是二级子进程在执行&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
      os<span class="token punctuation">.</span>_exit<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>  <span class="token comment"># 一级子进程退出</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    os<span class="token punctuation">.</span>wait<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 等待一级子进程退出</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>3、通过信号处理子进程退出
子进程退出时会发送信号给父进程，如果父进程忽略子进程信号，则系统就会自动处理子进程退出。父进程在创建子进程前调用 <code>signal.signal(signal.SIGCHLD,signal.SIG_IGN)</code></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> os
<span class="token keyword">import</span> signal
<span class="token comment"># 子进程退出时父进程会忽略,此时子进程自动由系统处理</span>
signal<span class="token punctuation">.</span>signal<span class="token punctuation">(</span>signal<span class="token punctuation">.</span>SIGCHLD<span class="token punctuation">,</span>signal<span class="token punctuation">.</span>SIG_IGN<span class="token punctuation">)</span>
pid <span class="token operator">=</span> os<span class="token punctuation">.</span>fork<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> pid <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
    os<span class="token punctuation">.</span>_exit<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">#子进程退出</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
    <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
      <span class="token keyword">pass</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="multiprocessing"><a href="#multiprocessing" class="header-anchor">#</a> multiprocessing</h3> <p>由于 Windows 没有 <code>fork</code>  调用，而如果我们需要在 Windows 上用 Python 编写多进程的程序，我们就需要使用到 <code>multiprocessing</code>  模块。</p> <table><thead><tr><th style="text-align:center;">multiprocessing 常用方法</th> <th style="text-align:center;">简介</th></tr></thead> <tbody><tr><td style="text-align:center;"><code>cpu_count()</code></td> <td style="text-align:center;">查看当前机器 CPU 核心数量</td></tr> <tr><td style="text-align:center;"><code>active_children()</code></td> <td style="text-align:center;">获得目前所有的运行的进程</td></tr></tbody></table> <h4 id="process"><a href="#process" class="header-anchor">#</a> Process</h4> <p>在 multiprocessing 中，每一个进程都用一个 Process 类来表示。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># 创建进程 </span>
<span class="token comment"># group 分组，很少使用</span>
<span class="token comment"># target 调用对象(方法名)</span>
<span class="token comment"># name 别名</span>
<span class="token comment"># args 调用对象的参数元组</span>
<span class="token comment"># kwargs 调用对象的字典</span>
multiprocessing<span class="token punctuation">.</span>Process<span class="token punctuation">(</span>group<span class="token punctuation">,</span> target<span class="token punctuation">,</span> name<span class="token punctuation">,</span> args<span class="token punctuation">,</span> kwargs<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><table><thead><tr><th style="text-align:center;">Process 常用方法</th> <th style="text-align:center;">简介</th></tr></thead> <tbody><tr><td style="text-align:center;"><code>start()</code></td> <td style="text-align:center;">开始运行进程</td></tr> <tr><td style="text-align:center;"><code>join()</code></td> <td style="text-align:center;">等待进程执行完毕后再执行后面的方法</td></tr></tbody></table> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> time<span class="token punctuation">,</span> sleep
<span class="token keyword">from</span> multiprocessing <span class="token keyword">import</span> Process

<span class="token keyword">def</span> <span class="token function">run</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;子进程[%s]开始执行&quot;</span> <span class="token operator">%</span> name<span class="token punctuation">)</span>
    sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;子进程[%s]执行结束&quot;</span> <span class="token operator">%</span> name<span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 创建子进程</span>
    p <span class="token operator">=</span> Process<span class="token punctuation">(</span>target<span class="token operator">=</span>run<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">&quot;测试&quot;</span><span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    p<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 开始执行</span>
    p<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 等待子进程结束后，再往下执行</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;父进程结束&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>子进程[测试]开始执行
子进程[测试]执行结束
父进程结束
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <p>全局变量在多个进程中不能共享，在子进程中修改全局变量对父进程中的全局变量没有影响。因为父进程在创建子进程时对全局变量做了一个备份，父进程中的全局变量与子进程的全局变量完全是不同的两个变量。</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> time<span class="token punctuation">,</span> sleep
<span class="token keyword">from</span> multiprocessing <span class="token keyword">import</span> Process

num <span class="token operator">=</span> <span class="token number">100</span>

<span class="token keyword">def</span> <span class="token function">run</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;子进程[%s]开始执行&quot;</span> <span class="token operator">%</span> name<span class="token punctuation">)</span>
    <span class="token keyword">global</span> num
    num <span class="token operator">+=</span> <span class="token number">1</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;子进程num：%d&quot;</span> <span class="token operator">%</span> num<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;子进程[%s]执行结束&quot;</span> <span class="token operator">%</span> name<span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 创建子进程</span>
    p <span class="token operator">=</span> Process<span class="token punctuation">(</span>target<span class="token operator">=</span>run<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">&quot;测试&quot;</span><span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    p<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 开始执行</span>
    p<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 等待子进程结束后，再往下执行</span>
    <span class="token comment"># 在子进程中修改全局变量对父进程中的全局变量没有影响</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;父进程结束 num：%s&quot;</span> <span class="token operator">%</span> num<span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>子进程[测试]开始执行
子进程num：101
子进程[测试]执行结束
父进程结束 num：100
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <h3 id="进程池"><a href="#进程池" class="header-anchor">#</a> 进程池</h3> <p>如果要启动大量的子进程，可以用进程池的方式批量创建子进程</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># 创建进程池</span>
<span class="token comment"># num 参数标识可以同时执行的进程数量，默认大小是 CPU 核心数</span>
Pool<span class="token punctuation">(</span>num<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><table><thead><tr><th style="text-align:center;">Pool 常用方法</th> <th style="text-align:center;">简介</th></tr></thead> <tbody><tr><td style="text-align:center;">apply_async()</td> <td style="text-align:center;">创建进程并放入进程池统一管理</td></tr> <tr><td style="text-align:center;">close()</td> <td style="text-align:center;">在 <code>close</code>  之后不能再继续往进程池添加新的进程</td></tr> <tr><td style="text-align:center;">join()</td> <td style="text-align:center;">进程池使用 <code>join</code>  方法前必须先 <code>close</code></td></tr></tbody></table> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> time<span class="token punctuation">,</span> sleep
<span class="token keyword">import</span> os
<span class="token keyword">from</span> multiprocessing<span class="token punctuation">.</span>pool <span class="token keyword">import</span> Pool

<span class="token keyword">def</span> <span class="token function">run</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;%s子进程开始，进程ID：%d&quot;</span> <span class="token operator">%</span> <span class="token punctuation">(</span>name<span class="token punctuation">,</span> os<span class="token punctuation">.</span>getpid<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;%s子进程结束，进程ID：%d&quot;</span> <span class="token operator">%</span> <span class="token punctuation">(</span>name<span class="token punctuation">,</span> os<span class="token punctuation">.</span>getpid<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 创建进程池，表示可以同时执行的进程数量。默认大小是CPU的核心数</span>
    p <span class="token operator">=</span> Pool<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment"># 创建进程，放入进程池统一管理</span>
        p<span class="token punctuation">.</span>apply_async<span class="token punctuation">(</span>run<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment"># 在调用join()之前必须要先close()</span>
    p<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># 进程池对象调用join，会等待进程池中所有的子进程结束完毕再去结束父进程</span>
    p<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;父进程结束。&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h3 id="自定义进程类"><a href="#自定义进程类" class="header-anchor">#</a> 自定义进程类</h3> <p>自定义进程类，继承 Process 类，重写 run 方法就可以了</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> sleep
<span class="token keyword">from</span> multiprocessing <span class="token keyword">import</span> Process

<span class="token keyword">class</span> <span class="token class-name">MyProcess</span><span class="token punctuation">(</span>Process<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> name<span class="token punctuation">)</span><span class="token punctuation">:</span>
        Process<span class="token punctuation">.</span>__init__<span class="token punctuation">(</span>self<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>name <span class="token operator">=</span> name

    <span class="token keyword">def</span> <span class="token function">run</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;子进程启动&quot;</span><span class="token punctuation">)</span>
        sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;子进程结束&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    p <span class="token operator">=</span> MyProcess<span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span>
    p<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 自动调用MyProcess的run()方法</span>
    p<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;父进程结束&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>子进程启动
子进程结束
父进程结束
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <h3 id="进程间通讯"><a href="#进程间通讯" class="header-anchor">#</a> 进程间通讯</h3> <p>multiprocessing 提供了多种方法来进行进程间通讯，如 Queue、Pipe、Manager</p> <div class="custom-block danger"><p class="custom-block-title">注意</p> <p>这里必须使用 multiprocessing 模块中的 Queue、Pipe、Manager</p></div> <h4 id="queue"><a href="#queue" class="header-anchor">#</a> Queue</h4> <p>多进程安全的队列，可以实现多个进程之间的数据传递，一个进程将数据写入队列，再由队列将数据交给另一进程，<font color="red">不能用于进程池</font>，在进程池中可以使用 Manager 下的 Queue，用法相似</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># 创建 Queue</span>
<span class="token comment"># maxsize 队列中允许的最大项数，省略则无大小限制</span>
Queue<span class="token punctuation">(</span>maxsize<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><table><thead><tr><th style="text-align:center;">Queue 常用方法</th> <th style="text-align:center;">介绍</th></tr></thead> <tbody><tr><td style="text-align:center;">get([block[,timeout]])</td> <td style="text-align:center;">返回队列中的一个项目，如果队列为空方法将阻塞直到队列中有项目可用为止 <br> block 控制阻塞行为，默认为 True，设置为 False 将引发异常 <br> timeout 指定在阻塞模式中等待项目可用的时间长短，超时引发异常</td></tr> <tr><td style="text-align:center;">get_nowait()</td> <td style="text-align:center;">同 get (False) 方法</td></tr> <tr><td style="text-align:center;">put(item[,block[,timeout]])</td> <td style="text-align:center;">将 item 放入队列，如果队列已满将阻塞至有空间可用为止<br> block 控制阻塞行为，默认为 True，设置为 False 将引发异常 <br> timeout 指定在阻塞模式中等待可用空间的时间长短，超时引发异常</td></tr> <tr><td style="text-align:center;">qsize()</td> <td style="text-align:center;">返回队列中目前项目的正确数量</td></tr> <tr><td style="text-align:center;">empty()</td> <td style="text-align:center;">检查队列是否为空</td></tr> <tr><td style="text-align:center;">full()</td> <td style="text-align:center;">检查队列是否已满</td></tr> <tr><td style="text-align:center;">close()</td> <td style="text-align:center;">关闭队列，防止队列中加入更多数据</td></tr></tbody></table> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> time<span class="token punctuation">,</span> sleep
<span class="token keyword">from</span> multiprocessing <span class="token keyword">import</span> Process<span class="token punctuation">,</span>Queue

<span class="token keyword">def</span> <span class="token function">runWrite</span><span class="token punctuation">(</span>q<span class="token punctuation">)</span><span class="token punctuation">:</span>
    q<span class="token punctuation">.</span>put<span class="token punctuation">(</span><span class="token string">&quot;测试数据&quot;</span><span class="token punctuation">)</span>
    sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">runRead</span><span class="token punctuation">(</span>q<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
        value <span class="token operator">=</span> q<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;读取到%s&quot;</span> <span class="token operator">%</span> value<span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 父进程创建队列，并传递给子进程</span>
    q <span class="token operator">=</span> Queue<span class="token punctuation">(</span><span class="token punctuation">)</span>
    pw <span class="token operator">=</span> Process<span class="token punctuation">(</span>target<span class="token operator">=</span>runWrite<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span>q<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    pr <span class="token operator">=</span> Process<span class="token punctuation">(</span>target<span class="token operator">=</span>runRead<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span>q<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    pw<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
    pr<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
    pw<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># pr进程是一个死循环，无法等待其结束，只能强行结束（写进程结束了，所以读进程也可以结束了）</span>
    pr<span class="token punctuation">.</span>terminate<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;父进程结束&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>读取到测试数据
父进程结束
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></section></div></div> <h4 id="pipe"><a href="#pipe" class="header-anchor">#</a> Pipe</h4> <p>Pipe 在内存中开辟管道空间，生成管道操作对象，两个进程使用同一个管道对象进行读写即可实现通信， Pipe 的性能高于 Queue，因为 Queue 中加了锁，Pipe 只能用于两个进程通信，在只需要两个进程通信的情况下优先考虑该方法</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># 创建 Pipe</span>
<span class="token comment"># duplex 管道工作模式，True 为双向，False 为单向，默认工作模式是双向</span>
<span class="token comment"># p1,p2 双向模式 p1，p2 均可读写，单向模式 p1 只读 p2 只写</span>
p1<span class="token punctuation">,</span>p2 <span class="token operator">=</span> Pipe<span class="token punctuation">(</span>duplex<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th style="text-align:center;">Pipe 方法</th> <th style="text-align:center;">介绍</th></tr></thead> <tbody><tr><td style="text-align:center;">recv()</td> <td style="text-align:center;">从管道获取内容</td></tr> <tr><td style="text-align:center;">send(data)</td> <td style="text-align:center;">向管道写入内容</td></tr></tbody></table> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> time<span class="token punctuation">,</span> sleep
<span class="token keyword">from</span> multiprocessing <span class="token keyword">import</span> Process<span class="token punctuation">,</span> Pipe

<span class="token keyword">def</span> <span class="token function">runWrite</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">:</span>
    p<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token string">&quot;测试数据&quot;</span><span class="token punctuation">)</span>
    sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">runRead</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
        value <span class="token operator">=</span> p<span class="token punctuation">.</span>recv<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;读取到%s&quot;</span> <span class="token operator">%</span> value<span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    p1<span class="token punctuation">,</span> p2 <span class="token operator">=</span> Pipe<span class="token punctuation">(</span><span class="token punctuation">)</span>
    pw <span class="token operator">=</span> Process<span class="token punctuation">(</span>target<span class="token operator">=</span>runWrite<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span>p1<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    pr <span class="token operator">=</span> Process<span class="token punctuation">(</span>target<span class="token operator">=</span>runRead<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span>p2<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    pw<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
    pr<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
    pw<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># pr进程是一个死循环，无法等待其结束，只能强行结束（写进程结束了，所以读进程也可以结束了）</span>
    pr<span class="token punctuation">.</span>terminate<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;父进程结束&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>读取到测试数据
父进程结束</p></section></div></div> <h4 id="manager"><a href="#manager" class="header-anchor">#</a> Manager</h4> <p>Manager 控制了一个 server 进程，此进程包含的 python 对象 (list, dict, Namespace, Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Queue, Value 和 Array) 可以被其他的进程通过 proxies 来访问实现数据共享，一般需要通过加锁 Lock 来确保数据的安全性。当使用进程池需要实现通讯时需要使用该方法</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> time<span class="token punctuation">,</span> sleep
<span class="token keyword">from</span> multiprocessing <span class="token keyword">import</span> Process<span class="token punctuation">,</span> Manager<span class="token punctuation">,</span> Pool

<span class="token keyword">def</span> <span class="token function">runWrite</span><span class="token punctuation">(</span>q<span class="token punctuation">)</span><span class="token punctuation">:</span>
    q<span class="token punctuation">.</span>put<span class="token punctuation">(</span><span class="token string">&quot;测试数据&quot;</span><span class="token punctuation">)</span>
    sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">runRead</span><span class="token punctuation">(</span>q<span class="token punctuation">)</span><span class="token punctuation">:</span>
    value <span class="token operator">=</span> q<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;读取到%s&quot;</span> <span class="token operator">%</span> value<span class="token punctuation">)</span>
    sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    queue <span class="token operator">=</span> Manager<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Queue<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 创建队列</span>
    pool <span class="token operator">=</span> Pool<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>  <span class="token comment"># 创建线程池</span>
    pool<span class="token punctuation">.</span>apply_async<span class="token punctuation">(</span>runWrite<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span>queue<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    pool<span class="token punctuation">.</span>apply_async<span class="token punctuation">(</span>runRead<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span>queue<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    pool<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    pool<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;父进程结束&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>读取到测试数据
父进程结束</p></section></div></div> <p>多个进程共享对象，以 dict 为例
为了确保数据安全使用 Lock 加锁，注意所有进程必须使用相同的 Lock 对象加锁</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> time<span class="token punctuation">,</span> sleep
<span class="token keyword">from</span> multiprocessing <span class="token keyword">import</span> Process<span class="token punctuation">,</span> Manager<span class="token punctuation">,</span> Pool

<span class="token keyword">def</span> <span class="token function">run_test</span><span class="token punctuation">(</span>dic<span class="token punctuation">,</span> key<span class="token punctuation">,</span> lock<span class="token punctuation">)</span><span class="token punctuation">:</span>
    lock<span class="token punctuation">.</span>acquire<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 加锁</span>
    dic<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">&quot;测试数据&quot;</span>
    lock<span class="token punctuation">.</span>release<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 解锁</span>
    sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    dic <span class="token operator">=</span> Manager<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token builtin">dict</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 创建共享的 dict</span>
    lock <span class="token operator">=</span> Manager<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Lock<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 创建锁</span>
    pool <span class="token operator">=</span> Pool<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> key <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
        pool<span class="token punctuation">.</span>apply_async<span class="token punctuation">(</span>run_test<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span>dic<span class="token punctuation">,</span> key<span class="token punctuation">,</span> lock<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    pool<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    pool<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>dic<span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>{'a': '测试数据', 'b': '测试数据', 'd': '测试数据', 'c': '测试数据'}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <h2 id="多线程"><a href="#多线程" class="header-anchor">#</a> 多线程</h2> <p>Python 的标准库提供了两个模块：_thread 和 threading，_thread 是低级模块，threading 是高级模块，对 _thread 进行了封装。绝大多数情况下，我们只需要使用 threading 这个高级模块。
Python 的线程虽然是真正的线程，但解释器执行代码时，有一个 GIL 锁，任何 Python 线程执行前，必须先获得 GIL 锁，每执行 100 条字节码，解释器就自动释放 GIL 锁，让别的线程有机会执行。这个 GIL 全局锁实际上把所有线程的执行代码都给上了锁，所以多线程在 Python 中只能交替执行，即使 100 个线程跑在 100 核 CPU 上，也只能用到 1 个核。所以当需要多核任务时一般使用多进程处理</p> <table><thead><tr><th style="text-align:center;">threading 方法</th> <th style="text-align:center;">简介</th></tr></thead> <tbody><tr><td style="text-align:center;"><code>current_thread()</code></td> <td style="text-align:center;">返回当前对应调用者的控制线程的 Thread 对象</td></tr> <tr><td style="text-align:center;"><code>main_thread()</code></td> <td style="text-align:center;">返回主 Thread 对象</td></tr></tbody></table> <h3 id="thread"><a href="#thread" class="header-anchor">#</a> Thread</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># 创建线程</span>
<span class="token comment"># group 分组，很少使用</span>
<span class="token comment"># target 调用对象(方法名)</span>
<span class="token comment"># name 别名</span>
<span class="token comment"># args 调用对象的参数元组</span>
<span class="token comment"># kwargs 调用对象的字典</span>
threading<span class="token punctuation">.</span>Thread<span class="token punctuation">(</span><span class="token punctuation">[</span>group<span class="token punctuation">[</span><span class="token punctuation">,</span>target<span class="token punctuation">[</span><span class="token punctuation">,</span>name<span class="token punctuation">[</span><span class="token punctuation">,</span>args<span class="token punctuation">[</span><span class="token punctuation">,</span>kwargs<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><table><thead><tr><th style="text-align:center;">Thread 常用方法</th> <th style="text-align:center;">简介</th></tr></thead> <tbody><tr><td style="text-align:center;"><code>start()</code></td> <td style="text-align:center;">开始线程任务</td></tr> <tr><td style="text-align:center;"><code>join()</code></td> <td style="text-align:center;">阻塞调用该方法的线程，直到被调用 join 方法的线程终结</td></tr></tbody></table> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> time<span class="token punctuation">,</span> sleep
<span class="token keyword">import</span> threading

<span class="token keyword">def</span> <span class="token function">run_test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;当前线程,%s&quot;</span> <span class="token operator">%</span> threading<span class="token punctuation">.</span>current_thread<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span>
    sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;当前线程,%s&quot;</span> <span class="token operator">%</span> threading<span class="token punctuation">.</span>current_thread<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span>
    thread <span class="token operator">=</span> threading<span class="token punctuation">.</span>Thread<span class="token punctuation">(</span>target<span class="token operator">=</span>run_test<span class="token punctuation">)</span>
    thread<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
    thread<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;当前线程,%s&quot;</span> <span class="token operator">%</span> threading<span class="token punctuation">.</span>current_thread<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>当前线程,MainThread
当前线程,Thread-1
当前线程,MainThread
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <h3 id="通过继承-thread-的方式创建线程"><a href="#通过继承-thread-的方式创建线程" class="header-anchor">#</a> 通过继承 Thread 的方式创建线程</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> time<span class="token punctuation">,</span> sleep
<span class="token keyword">import</span> threading

<span class="token keyword">class</span> <span class="token class-name">MyThread</span><span class="token punctuation">(</span>threading<span class="token punctuation">.</span>Thread<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 实现 run 方法</span>
    <span class="token keyword">def</span> <span class="token function">run</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;当前线程,%s&quot;</span> <span class="token operator">%</span> threading<span class="token punctuation">.</span>current_thread<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span>
        sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;当前线程,%s&quot;</span> <span class="token operator">%</span> threading<span class="token punctuation">.</span>current_thread<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span>
    thread <span class="token operator">=</span> MyThread<span class="token punctuation">(</span><span class="token punctuation">)</span>
    thread<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
    thread<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;当前线程,%s&quot;</span> <span class="token operator">%</span> threading<span class="token punctuation">.</span>current_thread<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>name<span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>当前线程,MainThread
当前线程,Thread-1
当前线程,MainThread
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <h3 id="线程池"><a href="#线程池" class="header-anchor">#</a> 线程池</h3> <p>线程池的基类是 concurrent.futures 模块中的  <code>Executor</code> ， <code>Executor</code>  提供了两个子类  <code>ThreadPoolExecutor</code>  和  <code>ProcessPoolExecutor</code> ，其中  <code>ThreadPoolExecutor</code>  用于创建线程池   <code>ProcessPoolExecutor</code>  用于创建进程池。</p> <div class="custom-block tip"><p class="custom-block-title">线程池具有的功能特性</p> <p>自动调度线程<br>
主线程可以获取某一个线程（或者任务的）的状态，以及返回值。<br>
当一个线程完成的时候，主线程能够立即知道。<br>
让多线程和多进程的编码接口一致</p></div> <p>由于多线程和多进程的编码接口一致，下面介绍的方法只需将  <code>ThreadPoolExecutor</code>  替换为  <code>ProcessPoolExecutor</code>  即可换为进程池</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token comment"># 创建线程池</span>
<span class="token comment"># max_workers 设置线程池中最多能同时运行的线程数目</span>
ThreadPoolExecutor<span class="token punctuation">(</span>max_workers<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><table><thead><tr><th style="text-align:center;">Executor 常用方法</th> <th style="text-align:center;">简介</th></tr></thead> <tbody><tr><td style="text-align:center;"><code>submit()</code></td> <td style="text-align:center;">提交任务到线程池 / 进程池</td></tr> <tr><td style="text-align:center;"><code>map(func, *iterables)</code></td> <td style="text-align:center;">为 iterables 的每个元素启动一个线程并发执行 func 函数，井收集每个线程的执行结果</td></tr> <tr><td style="text-align:center;"><code>shutdown()</code></td> <td style="text-align:center;">关闭线程池 / 进程池</td></tr></tbody></table> <p>程序将任务提交 (submit) 给线程池后， <code>submit</code>  方法会返回一个  <code>Future</code>  对象， <code>Future</code>  类主要用于获取线程任务的返回值。由于线程任务会在新线程中以异步方式执行，因此线程执行的函数相当于一个 “将来完成” 的任务，所以 Python 使用  <code>Future</code>  来代表</p> <table><thead><tr><th style="text-align:center;">Future 常用方法</th> <th style="text-align:center;">简介</th></tr></thead> <tbody><tr><td style="text-align:center;"><code>cancel()</code></td> <td style="text-align:center;">取消线程任务，已经开始运行的任务无法取消</td></tr> <tr><td style="text-align:center;"><code>cancelled()</code></td> <td style="text-align:center;">返回任务是否取消成功</td></tr> <tr><td style="text-align:center;"><code>running()</code></td> <td style="text-align:center;">返回任务是否正在运行</td></tr> <tr><td style="text-align:center;"><code>done()</code></td> <td style="text-align:center;">返回任务是否已经被取消或执行完成</td></tr> <tr><td style="text-align:center;"><code>result(timeout)</code></td> <td style="text-align:center;">获取任务最后返回的结果，如果未完成将阻塞当前线程<br>timeout 指定最多阻塞多少秒</td></tr> <tr><td style="text-align:center;"><code>exception()</code></td> <td style="text-align:center;">返回任务引发的异常，如果无异常返回 None</td></tr> <tr><td style="text-align:center;"><code>add_done_callback(fn)</code></td> <td style="text-align:center;">为线程任务注册回调函数，任务结束会自动调用该函数<br>fn 回调函数，回调时会将当前 Future 对象作为参数传入</td></tr></tbody></table> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> time<span class="token punctuation">,</span> sleep
<span class="token keyword">from</span> concurrent<span class="token punctuation">.</span>futures <span class="token keyword">import</span> ThreadPoolExecutor<span class="token punctuation">,</span> Future

<span class="token keyword">def</span> <span class="token function">run</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">:</span>
    sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token string">&quot;线程 %s 已经执行完毕&quot;</span> <span class="token operator">%</span> name

<span class="token keyword">def</span> <span class="token function">result</span><span class="token punctuation">(</span>future<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>future<span class="token punctuation">.</span>result<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 创建线程池</span>
    <span class="token comment"># 使用 with 可以在任务结束时自动关闭线程池</span>
    <span class="token keyword">with</span> ThreadPoolExecutor<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token keyword">as</span> pool<span class="token punctuation">:</span>
        future1 <span class="token operator">=</span> pool<span class="token punctuation">.</span>submit<span class="token punctuation">(</span>run<span class="token punctuation">,</span> <span class="token string">&quot;A&quot;</span><span class="token punctuation">)</span>
        future1<span class="token punctuation">.</span>add_done_callback<span class="token punctuation">(</span>result<span class="token punctuation">)</span>
        future2 <span class="token operator">=</span> pool<span class="token punctuation">.</span>submit<span class="token punctuation">(</span>run<span class="token punctuation">,</span> <span class="token string">&quot;B&quot;</span><span class="token punctuation">)</span>
        future2<span class="token punctuation">.</span>add_done_callback<span class="token punctuation">(</span>result<span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;---------&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>---------
线程 A 已经执行完毕
线程 B 已经执行完毕
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <p>使用  <code>map</code>  方法启动线程</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> time <span class="token keyword">import</span> time<span class="token punctuation">,</span> sleep
<span class="token keyword">from</span> concurrent<span class="token punctuation">.</span>futures <span class="token keyword">import</span> ThreadPoolExecutor<span class="token punctuation">,</span> Future

<span class="token keyword">def</span> <span class="token function">run</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">:</span>
    sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token string">&quot;线程 %s 已经执行完毕&quot;</span> <span class="token operator">%</span> name

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 创建线程池</span>
    <span class="token comment"># 使用 with 可以在任务结束时自动关闭线程池</span>
    <span class="token keyword">with</span> ThreadPoolExecutor<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token keyword">as</span> pool<span class="token punctuation">:</span>
        results <span class="token operator">=</span> pool<span class="token punctuation">.</span><span class="token builtin">map</span><span class="token punctuation">(</span>run<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">&quot;A&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;B&quot;</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;---------&quot;</span><span class="token punctuation">)</span>
        <span class="token keyword">for</span> r <span class="token keyword">in</span> results<span class="token punctuation">:</span>
            <span class="token keyword">print</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span>

<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>---------
线程 A 已经执行完毕
线程 B 已经执行完毕
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <h2 id="协程"><a href="#协程" class="header-anchor">#</a> 协程</h2> <p>协程 (微线程) 是比线程更轻量化的存在，一个线程也可以拥有多个协程，协程不是被操作系统内核所管理，而完全是由程序所控制</p> <div class="custom-block tip"><p class="custom-block-title">协程的好处</p> <p>无需线程上下文切换的开销<br>
无需原子操作锁定及同步的开销<br>
方便切换控制流，简化编程模型<br>
高并发 + 高扩展性 + 低成本：一个 CPU 支持上万的协程都不是问题。所以很适合用于高并发处理。</p></div> <div class="custom-block warning"><p class="custom-block-title">协程的缺点</p> <p>无法利用多核资源<br>
进行阻塞操作会阻塞掉整个程序</p></div> <h3 id="通过生成器-generator-实现"><a href="#通过生成器-generator-实现" class="header-anchor">#</a> 通过生成器 (Generator) 实现</h3> <p>模拟一个下载器  <code>download</code>  与管理者  <code>manager</code>  ， <code>manager</code>  将下载地址发送给  <code>download</code>  下载，同时  <code>download</code>  下载后将结果返回  <code>manager</code> ，这里  <code>download</code>  是一个生成器</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">download</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    result <span class="token operator">=</span> <span class="token string">&quot;&quot;</span>
    <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
        <span class="token comment"># 获取下载地址,返回下载结果</span>
        url <span class="token operator">=</span> <span class="token keyword">yield</span> result
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;开始下载 %s 的内容&quot;</span> <span class="token operator">%</span> url<span class="token punctuation">)</span>
        result <span class="token operator">=</span> <span class="token string">&quot;这是 %s 下载的结果&quot;</span> <span class="token operator">%</span> url

<span class="token keyword">def</span> <span class="token function">manager</span><span class="token punctuation">(</span>load<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 通过 send(None) 启动下载器</span>
    load<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token boolean">None</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> url <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token string">&quot;url1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;url2&quot;</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
        <span class="token comment"># 发送给下载器下载,获取下载结果</span>
        result <span class="token operator">=</span> load<span class="token punctuation">.</span>send<span class="token punctuation">(</span>url<span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
    load<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    
<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    download <span class="token operator">=</span> download<span class="token punctuation">(</span><span class="token punctuation">)</span>
    manager<span class="token punctuation">(</span>download<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>开始下载 url1 的内容
这是 url1 下载的结果
开始下载 url2 的内容
这是 url2 下载的结果
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <p>首先调用  <code>send(None)</code>  启动生成器，然后获取数据并通过  <code>send()</code>  切换到  <code>download</code>  执行，
 <code>download</code>  通过  <code>yield</code>  拿到消息处理后又通过  <code>yield</code>  把结果传回， <code>manager</code>  拿到  <code>download</code>  处理的结果继续生产下一条消息，最后通过  <code>close()</code>  关闭  <code>download</code>  来结束。整个流程由一个线程执行， <code>download</code>  和  <code>manager</code>  协作完成任务所以称为 “协程”</p> <h3 id="通过-greenlet-实现"><a href="#通过-greenlet-实现" class="header-anchor">#</a> 通过 greenlet 实现</h3> <p>生成器可以实现协程但并不完善也不易于理解，我们可以通过  <code>greenlet</code>  来更好的实现，使用前需先下载  <code>greenlet</code>  模块</p> <div class="language-shell-session line-numbers-mode"><pre class="language-shell-session"><code><span class="token output">pip install greenlet
</span></code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><table><thead><tr><th style="text-align:center;">常用方法</th> <th style="text-align:center;">简介</th></tr></thead> <tbody><tr><td style="text-align:center;"><code>greenlet(func)</code></td> <td style="text-align:center;">启动一个协程</td></tr> <tr><td style="text-align:center;"><code>switch()</code></td> <td style="text-align:center;">切换协程</td></tr></tbody></table> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> greenlet <span class="token keyword">import</span> greenlet

<span class="token keyword">def</span> <span class="token function">download</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;开始下载 %s 的内容&quot;</span> <span class="token operator">%</span> url<span class="token punctuation">)</span>
        result <span class="token operator">=</span> <span class="token string">&quot;这是 %s 下载的结果&quot;</span> <span class="token operator">%</span> url
        <span class="token comment"># 切换 manager 运行</span>
        url <span class="token operator">=</span> manager<span class="token punctuation">.</span>switch<span class="token punctuation">(</span>result<span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">manager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">for</span> url <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token string">&quot;url1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;url2&quot;</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
        <span class="token comment"># 切换到下载器</span>
        result <span class="token operator">=</span> download<span class="token punctuation">.</span>switch<span class="token punctuation">(</span>url<span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>

<span class="token comment"># 创建协程</span>
download <span class="token operator">=</span> greenlet<span class="token punctuation">(</span>download<span class="token punctuation">)</span>
manager <span class="token operator">=</span> greenlet<span class="token punctuation">(</span>manager<span class="token punctuation">)</span>
<span class="token comment"># 切换到 manager 执行</span>
manager<span class="token punctuation">.</span>switch<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>开始下载 url1 的内容
这是 url1 下载的结果
开始下载 url2 的内容
这是 url2 下载的结果
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <h3 id="通过-gevent-实现"><a href="#通过-gevent-实现" class="header-anchor">#</a> 通过 gevent 实现</h3> <p><code>gevent</code>  封装了  <code>greenlet</code> ，并实现了遇到 IO 自动切换，使用前需先下载  <code>gevent</code>  模块</p> <div class="language-shell-session line-numbers-mode"><pre class="language-shell-session"><code><span class="token output">pip install gevent
</span></code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><table><thead><tr><th style="text-align:center;">gevent 常用方法</th> <th style="text-align:center;">简介</th></tr></thead> <tbody><tr><td style="text-align:center;"><code>spawn(func)</code></td> <td style="text-align:center;">启动一个协程</td></tr> <tr><td style="text-align:center;"><code>join()</code></td> <td style="text-align:center;">等待协程结束</td></tr> <tr><td style="text-align:center;"><code>joinall()</code></td> <td style="text-align:center;">可以等待多个协程结束，等同于对每一个都使用 join</td></tr> <tr><td style="text-align:center;"><code>monkey.patch_all()</code></td> <td style="text-align:center;">调用后可以将一些阻塞式的标准库如 socket、ssl、threading 和  select 等模块变为非阻塞，以便 gevent 自动识别切换协程，只对调用该方法后导入的模块有效</td></tr></tbody></table> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="python" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">import</span> gevent
<span class="token keyword">from</span> gevent <span class="token keyword">import</span> monkey
<span class="token comment"># 把当前程序的所有 IO 操作标记起来，否则模块无法知道 IO 操作</span>
monkey<span class="token punctuation">.</span>patch_all<span class="token punctuation">(</span><span class="token punctuation">)</span>  
<span class="token keyword">from</span> time <span class="token keyword">import</span> sleep

<span class="token keyword">def</span> <span class="token function">func1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;正在执行 func1 开始&quot;</span><span class="token punctuation">)</span>
    sleep<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;正在执行 func1 结束&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">func2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;正在执行 func2 开始&quot;</span><span class="token punctuation">)</span>
    sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;正在执行 func2 结束&quot;</span><span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">func3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;正在执行 func3 开始&quot;</span><span class="token punctuation">)</span>
    sleep<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;正在执行 func3 结束&quot;</span><span class="token punctuation">)</span>

g1 <span class="token operator">=</span> gevent<span class="token punctuation">.</span>spawn<span class="token punctuation">(</span>func1<span class="token punctuation">)</span>
g2 <span class="token operator">=</span> gevent<span class="token punctuation">.</span>spawn<span class="token punctuation">(</span>func2<span class="token punctuation">)</span>
g3 <span class="token operator">=</span> gevent<span class="token punctuation">.</span>spawn<span class="token punctuation">(</span>func3<span class="token punctuation">)</span>
<span class="token comment"># 等价于 g1.join()  g2.join()  g3.join()</span>
gevent<span class="token punctuation">.</span>joinall<span class="token punctuation">(</span><span class="token punctuation">[</span>g1<span class="token punctuation">,</span> g2<span class="token punctuation">,</span> g3<span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div></section> <section aria-hidden="true" id="输出" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language- line-numbers-mode"><pre class="language-text"><code>正在执行 func1 开始
正在执行 func2 开始
正在执行 func3 开始
正在执行 func1 结束
正在执行 func3 结束
正在执行 func2 结束
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></section></div></div> <p>这里使用  <code>sleep</code>  模拟耗时 IO 操作，可以看到在遇到  <code>sleep</code>  时自动切换到其他协程运行，效果与多线程类似。</p></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----> <!----></main> <!----> <div class="comments-wrapper" data-v-0cb1c278><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div><canvas id="vuepress-canvas-ribbon"></canvas><div class="my-popup" style="display:none;" data-v-3414bdeb><div class="my-popup-container" data-v-3414bdeb><div class="my-popup-exit" data-v-3414bdeb></div> <img src="" alt data-v-3414bdeb></div></div><div data-v-2ebabc0c></div><div class="kanbanniang" data-v-3b0f9b07><div class="banniang-container" style="display:;" data-v-3b0f9b07><div class="messageBox" style="right:68px;bottom:230px;display:none;" data-v-3b0f9b07>
      欢迎来到 KnightSama‘s Blog
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-3b0f9b07><i class="kbnfont kbn-ban-home ban-home" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-message message" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-close close" data-v-3b0f9b07></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-3b0f9b07><i class="kbnfont kbn-ban-info info" data-v-3b0f9b07></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-3b0f9b07></i></div> <canvas id="banniang" width="200" height="260" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-3b0f9b07></canvas></div> <div class="showBanNiang" style="display:none;" data-v-3b0f9b07>
    看板娘
  </div></div></div></div>
    <script src="/assets/js/app.a2483de7.js" defer></script><script src="/assets/js/4.0078dadf.js" defer></script><script src="/assets/js/1.c3e7d2b0.js" defer></script><script src="/assets/js/21.1fe46223.js" defer></script>
  </body>
</html>
